package com.tencent.iov.cowin.befsale.clue.service.cus.impl;

import com.tencent.iov.cowin.befsale.clue.controller.cus.assembler.DealerCustomerFollowPlanAssembler;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.AddCustomerFollowPlanRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.QueryCustomerFollowPlanRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.UpdateClueEmpRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.UpdateCustomerFollowPlanRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.response.CustomerFollowPlanResponse;
import com.tencent.iov.cowin.befsale.clue.domain.clue.po.Clue;
import com.tencent.iov.cowin.befsale.clue.domain.cus.po.OemCustomerFollowPlan;
import com.tencent.iov.cowin.befsale.clue.service.cus.CustomerFollowPlanService;
import com.tencent.iov.cowin.befsale.clue.service.cus.DealerCustomerFollowPlanService;
import com.tencent.iov.cowin.befsale.clue.service.cus.OemCustomerFollowPlanService;
import com.tencent.iov.cowin.befsale.clue.value.enums.FailStatusEnum;
import com.tencent.iov.cowin.befsale.clue.value.enums.FollowContentEnum;
import com.tencent.iov.cowin.befsale.clue.value.enums.FollowPlanStatusEnum;
import com.tencent.iov.cowin.befsale.clue.value.enums.PlanFollowTimeCodeEnum;
import com.tencent.iov.cowin.befsale.clue.value.utils.CustomerUtil;
import com.software.boot.common.exception.BusinessException;
import com.software.boot.common.exception.NEError;
import com.software.boot.common.util.BeanUtil;
import com.software.boot.common.util.DateUtil;
import com.software.boot.common.util.EmptyUtils;
import com.software.boot.common.util.ValidateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author xiamu
 * @Date 2022/1/14 18:21
 * @Version 1.0
 **/
@Slf4j
@Service
public class CustomerFollowPlanServiceImpl implements CustomerFollowPlanService {

    @Autowired
    private DealerCustomerFollowPlanService dealerCustomerFollowPlanService;
    @Autowired
    private OemCustomerFollowPlanService oemCustomerFollowPlanService;

    @Override
    public CustomerFollowPlanResponse getDetailById(Long id) {
        if (CustomerUtil.isDealer()) {
            return dealerCustomerFollowPlanService.getDetailById(id);
        }
        return oemCustomerFollowPlanService.getDetailById(id);
    }

    @Override
    public Long insert(AddCustomerFollowPlanRequest request) {
        DealerCustomerFollowPlanAssembler.initFollowPlanTime(request);
        if (CustomerUtil.isDealer()) {
            return dealerCustomerFollowPlanService.insert(request);
        }
        OemCustomerFollowPlan oemRequest = BeanUtil.copy(request, OemCustomerFollowPlan.class);
        return oemCustomerFollowPlanService.insert(oemRequest);
    }

    @Override
    public int insertBatch(List<AddCustomerFollowPlanRequest> requests) {
        if (CustomerUtil.isDealer()) {
            return dealerCustomerFollowPlanService.insertBatch(requests);
        }
        List<OemCustomerFollowPlan> oemRequests = BeanUtil.copyByList(requests, OemCustomerFollowPlan.class);
        return oemCustomerFollowPlanService.insertBatch(oemRequests);
    }

    @Override
    public int update(UpdateCustomerFollowPlanRequest request) {
        if (CustomerUtil.isDealer()) {
            return dealerCustomerFollowPlanService.update(request);
        }
        OemCustomerFollowPlan oemRequest = BeanUtil.copy(request, OemCustomerFollowPlan.class);
        return oemCustomerFollowPlanService.update(oemRequest);
    }

    @Override
    public CustomerFollowPlanResponse getOneByParam(QueryCustomerFollowPlanRequest request) {
        if (CustomerUtil.isDealer()) {
            return dealerCustomerFollowPlanService.getOneByParam(request);
        }
        return oemCustomerFollowPlanService.getOneByParam(request);
    }

    @Override
    public CustomerFollowPlanResponse insertOrUpdateByFailClue(Clue clue, UpdateClueEmpRequest request) {
        if (FailStatusEnum.AGREE.getCode().equals(request.getFailStatus())) {
            ValidateUtils.checkLongGTZero(request.getPlanFollowId(), "计划跟进Id不存在");
            CustomerFollowPlanResponse followPlanResponse = this.getDetailById(request.getPlanFollowId());
            if (EmptyUtils.isEmpty(followPlanResponse)) {
                throw new BusinessException(NEError.BUSINESS_ERROR, "不存在跟进计划");
            }
            UpdateCustomerFollowPlanRequest followPlanRequest = new UpdateCustomerFollowPlanRequest();
            followPlanRequest.setStatus(FollowPlanStatusEnum.CANCEL_FOLLOW.getCode());
            followPlanRequest.setId(request.getPlanFollowId());
            int result = this.update(followPlanRequest);
            if (result <= 0) {
                throw new BusinessException(NEError.BUSINESS_ERROR, "跟进计划取消失败");
            }
            followPlanResponse.setStatus(FollowPlanStatusEnum.CANCEL_FOLLOW.getCode());
            return followPlanResponse;
        }
        if (FailStatusEnum.ACTIVATE.getCode().equals(request.getFailStatus())) {
            AddCustomerFollowPlanRequest followPlanRequest = new AddCustomerFollowPlanRequest();
            followPlanRequest.setClueId(clue.getId());
            followPlanRequest.setCusId(clue.getCusId());
            followPlanRequest.setPlanFollowTime(DateUtil.addDate(new Date(), 1));
            followPlanRequest.setPlanFollowContent(FollowContentEnum.PRODUCT_INTRO.getCode().toString());
            followPlanRequest.setPlanFollowTimeCode(PlanFollowTimeCodeEnum.FOLLOW_TIME_CODE_FREE.getCode());
            DealerCustomerFollowPlanAssembler.initAddDealerCustomerFollowPlanRequest(followPlanRequest);
            followPlanRequest.setId(this.insert(followPlanRequest));
            return BeanUtil.copy(followPlanRequest, CustomerFollowPlanResponse.class);
        }
        return null;
    }
}
